<?php
/**
 * 商家储值管理
 */
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisCoupon;
use app\ais\model\AisStore;
use app\ais\model\AisCard;
use app\ais\model\AisCouponUser;
use app\ais\model\AisStoreUnion;
use filter\Filter;
use think\Db;

class Gmcard extends Base{

    protected $store;

    public function initialize() {
        parent::initialize();
        $this->isUserAuth();
        $this->store = AisStore::manageStore($this->user->id);
        if(empty($this->store)){
            exit(json_encode(['code' => 403,'msg'=>'无法找到该商家']));
        }
    }

   /**
    * 用户已领优惠券
    * @return void
    */
    public function index(){
        $this->apiSign();
        $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
        $condition[] = ['store_id','=',$this->store->id];
        $condition[] = ['is_end','=',0];
        $card = AisCard::with(['coupon'	=> function($query) {
            $query->field('id,amount,num,howmuch');
        }])->withCount('carduser')->withSum('cardorder','amount')->field('id,locktime,name,price,store_id,create_time,coupon_id')->where($condition)->select();
        if ($card->isEmpty()) {
            return enjson(204);
        }
        return enjson(200,$card);
    }

    /**
     * 读取活动优惠券
    */
    public function storeCardInfo(){
        $param['id'] = $this->request->param('id/d');
        $this->apiSign($param);
        $card = AisCard::where(['id' => $param['id']])->find();
        if (empty($card)) {
            return enjson(204);
        }
        $data = [
            'is_lock'  => $card->is_lock,
            'name'     => $card->name,
            'price'    => intval($card->price),
            'num'      => $card->coupon->num,
            'locktime' => date('Y/m/d',$card->locktime),
            'content'  => htmlspecialchars_decode($card->tips),
            'coupon' =>[
                'howmuch' => $card->coupon->howmuch,
                'amount'  => intval($card->coupon->amount),
                'name'    => $card->coupon->name,
                'tips'    => $card->coupon->tips,
            ]
        ];
        return enjson(200,$data);
    } 

     /**
     * 以下商家管理
     * ##########################################
     * @return void
     * 添加和修改优惠券
     */
    public function edit(){
        if(request()->isPost()){
            $param['id']       = $this->request->param('id');
            $param['name']     = $this->request->param('name');
            $param['price']    = $this->request->param('price/f',0);
            $param['amount']   = $this->request->param('amount/d',0);
            $param['howmuch']  = $this->request->param('howmuch/f',0);
            $param['num']      = $this->request->param('num/d');
            $param['locktime'] = $this->request->param('locktime/s');
            $param['tips']     = $this->request->param('tips/s');
            $param['content']  = $this->request->param('content/s');
            $this->apiSign($param);
            $validate = $this->validate($param,'Card.api');
            if(true !== $validate){
                return enjson(403,$validate);
            }
            //优惠券数据
            $coupon['types']             = 3;
            $coupon['is_lock']           = 1;
            $coupon['name']              = $param['name'];
            $coupon['amount']            = $param['amount'];
            $coupon['howmuch']           = $param['howmuch'];
            $coupon['tips']              = $param['tips'];
            $coupon['num']               = $param['num'];
            $coupon['member_miniapp_id'] = $this->member_miniapp_id;
            $coupon['store_id']          = $this->store->id;
            $coupon['citycode']         = $this->store->citycode;
            $coupon['update_time']       = time();
            $coupon['create_time']       = time();
            //判断是添加还是修改
            $card_rel = [];
            if(!empty($param['id'])){
                $card_rel = AisCard::where(['id' => $param['id']])->find();
            }
            if(empty($card_rel)){
                $coupon_rel = AisCoupon::create($coupon);
                if(!$coupon_rel){
                    return enjson(403,'储值活动创建失败');
                }
                $coupon_id = $coupon_rel->id;
            }else{
                $coupon_id = $card_rel->coupon_id;
                $coupon_rel = AisCoupon::where(['id' => $card_rel->coupon_id])->update($coupon);
                if(!$coupon_rel){
                    return enjson(403,'未找到储值活动');
                }
            }
            //判断是修改储值还是增加
            $card['store_id']          = $this->store->id;
            $card['member_miniapp_id'] = $this->member_miniapp_id;
            $card['name']              = $param['name'];
            $card['price']             = $param['price'];
            $card['tips']              = htmlspecialchars(base64_decode($param['content']));
            $card['coupon_id']         = $coupon_id;
            $card['locktime']          = strtotime($param['locktime']);
            $card['update_time']       = time();
            $card['create_time']       = time();
            if(empty($card_rel)){
                $rel = AisCard::create($card);
            }else{
                $rel = AisCard::where(['id' => $card_rel->id])->update($card);
            } 
            if($rel){
                return enjson(200,'储值活动创建成功');
            }
            return enjson(403,'未找到储值活动');
        }
    }

     //删除储值
    public function storeCardDel(){
        $param['id'] = $this->request->param('id/d',0);
        $this->apiSign($param);
        $card = AisCard::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $this->store->id,'id' => $param['id']])->find();
        if (empty($card)) {
            return enjson(404,'未找到储值活动');
        }
        $card->is_end = 1;
        $card->save();
        AisCoupon::where(['id' => $card->coupon_id])->update(['is_end' => 1]);
        return enjson(200,'删除成功');
    }
}